//-------------------------------------------------------------
#include "timer.h"
#include "log.h"
#include "thread.h"

#include <functional>
#include <vector>
#include <iostream>
namespace tt01{

learnco::Logger::ptr g_logger = learnco::LoggerMgr::GetInstance()->getLogger("user");
uint64_t count = 0;

    void test_timer()
    {
learnco::TimerManger TimerMgr;

        //添加定时任务
        TimerMgr.addTimer(1000, [&](){
            LEARNCO_LOG_DEBUG(g_logger) << " time1 count " << ++count; 
            
        }, true);

        TimerMgr.addTimer(2000, [&](){
            LEARNCO_LOG_DEBUG(g_logger) << " time2 count " << ++count; 
            
        }, true);

        //不断从定时任务中取出到期任务
        learnco::Thread t1([&](){
            while(true){
std::vector<std::function<void()>> cbs;
             uint64_t nexttime = TimerMgr.getNextTime();
             usleep(nexttime);
             TimerMgr.listExpiredCb(cbs);
             for(auto cb : cbs){
                 LEARNCO_LOG_DEBUG(g_logger) << "cb() " ;
                 cb();
             }
         }
        }, "TIMER");

        t1.join();
    }

} //namespace
//-----------------------------------------------------------


int main()
{
    tt01::test_timer();
    return 0;
}


